package com.ruoyi.project.module.kfc.controller;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;


import com.ruoyi.project.domain.YhAppointment;
import com.ruoyi.project.domain.YhEquipment;
import com.ruoyi.project.service.IYhAppointmentService;
import com.ruoyi.project.service.IYhEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备信息Controller
 * 
 * @author ruoyi
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/kfc/equipment")
public class KfcEquipmentController extends BaseController
{
    @Autowired
    private IYhEquipmentService yhEquipmentService;
    @Autowired
    private IYhAppointmentService yhAppointmentService;


    /**
     * 查询设备信息列表
     */
    @RequiresPermissions("kfc:equipment:list")
    @GetMapping("/list")
    public TableDataInfo list(YhEquipment yhEquipment)
    {
        startPage();
        yhEquipment.setNote3("0");
        List<YhEquipment> list = yhEquipmentService.selectYhEquipmentList(yhEquipment);
        return getDataTable(list);
    }

    /*导入预约信息列表*/
    @RequiresPermissions("kfc:equipment:import")
    @Log(title = "设备", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<YhEquipment> util = new ExcelUtil<YhEquipment>(YhEquipment.class);
        List<YhEquipment> mapList = util.importExcel(file.getInputStream());
        String message = yhEquipmentService.importContract(mapList, updateSupport);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<YhEquipment> util = new ExcelUtil<YhEquipment>(YhEquipment.class);
        util.importTemplateExcel(response, "导入数据");
    }


    /**
     * 导出设备信息列表
     */
    @RequiresPermissions("kfc:equipment:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhEquipment yhEquipment)
    {
        List<YhEquipment> list = yhEquipmentService.selectYhEquipmentList(yhEquipment);
        ExcelUtil<YhEquipment> util = new ExcelUtil<YhEquipment>(YhEquipment.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    @RequiresPermissions("kfc:equipment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yhEquipmentService.selectYhEquipmentById(id));
    }

    /**
     * 新增设备信息
     */
    @RequiresPermissions("kfc:equipment:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhEquipment yhEquipment)
    {
        YhEquipment yhEquipment1=new YhEquipment();
        yhEquipment1.setInstrumentId(yhEquipment.getInstrumentId());
        List<YhEquipment> list = yhEquipmentService.selectYhEquipmentList(yhEquipment1);
        if(list != null && !list.isEmpty()) {
            if (list.get(0).getInstrumentId().equals(yhEquipment.getInstrumentId())) {
                throw new ServiceException("设备编号重复，请重新输入！");
            }
        }
        yhEquipment.setNote3("0");
        return toAjax(yhEquipmentService.insertYhEquipment(yhEquipment));
    }

    /**
     * 修改设备信息
     */
    @RequiresPermissions("kfc:equipment:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhEquipment yhEquipment)
    {
        YhAppointment yhAppointment=new YhAppointment();
        yhAppointment.setInstrumentId(yhEquipment.getInstrumentId());
        List<YhAppointment> list = yhAppointmentService.selectYhAppointmentList(yhAppointment);

        System.out.println(list.size());
        for(int i=0;i<list.size();i++){
            list.get(i).setInstrumentName(yhEquipment.getInstrumentName());
            list.get(i).setEquipmentType(yhEquipment.getEquipmentType());
            list.get(i).setApplicationDepartment(yhEquipment.getApplicationDepartment());
            list.get(i).setModelNumber(yhEquipment.getModelNumber());
            list.get(i).setSpecification(yhEquipment.getSpecification());
            list.get(i).setUnitPrice(yhEquipment.getUnitPrice());
            list.get(i).setClassifyNumber(yhEquipment.getClassifyNumber());
            list.get(i).setPurchaseDate(yhEquipment.getPurchaseDate());
            list.get(i).setManufacturers(yhEquipment.getManufacturers());
            list.get(i).setReceiverId(yhEquipment.getReceiverId());
            list.get(i).setReceiverName(yhEquipment.getReceiverName());
            list.get(i).setStorageDate(yhEquipment.getStorageDate());
            list.get(i).setStorageLocation(yhEquipment.getStorageLocation());
            list.get(i).setNote3(yhEquipment.getNote3());
        }
        for(int i=0;i<list.size();i++){
           yhAppointmentService.updateYhAppointment(list.get(i));
        }
        return toAjax(yhEquipmentService.updateYhEquipment(yhEquipment));
    }

    /**
     * 删除设备信息
     */
    @RequiresPermissions("kfc:equipment:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {


        return toAjax(yhEquipmentService.deleteYhEquipmentByIds(ids));
    }

    /**
     * 通过设备类型查询设备名称
     */
    @RequestMapping ("/test/{equipmentType}")
    public AjaxResult test(@PathVariable ("equipmentType") String equipmentType)
    {
        System.out.println("2222");
        List<YhEquipment> equipType = yhEquipmentService.selectYhEquipmentByType(equipmentType);
        System.out.println(equipType+"111");
        return success(equipType);
    }

}
